var fs = require('fs');
var myArgs = process.argv.slice(1);

var input = [];
var i = 0;
var text;
var answer;

var readline = require('readline');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});


interface();


function interface() {

    rl.question("commande ? ", function(answer) {
        switch (answer) {

            case "exit":
                setTimeout(rl.close(), 1000);
                break;
            case "afficher":
                afficher();
                rl.close();
                //interface();
                break;
            case "add":
                rl.question("Entrez le prenom :", function(pnom) {
                    rl.question("Entrez le nom :", function(nom) {
                        rl.question("Entrez le telephone(format 10 chiffres a la suite) : ", function(tel) {
                            rl.question("Entrez le mail :", function(mail) {
                                rl.question("Entrez la societe :", function(org) {
                                    ajout(pnom, nom, tel, org, mail);
                                    console.log("nouveau contact en cours d'ajout, attendez quelques secondes");
                                    setTimeout(rl.close(), 1000);
                                    //interface();
                                }
                                )
                            }
                            )
                        }
                        )
                    }
                    )
                }
                )


                break;
            case "delete":
                var spec3 = require('./spec3.js');
                break;
            case "fusion":
                var spec5 = require('./spec5.js');
                break;
            case "export":
                var spec6 = require('./csv.js');
                break;
            default:
                console.log("commande non valide");
                setTimeout(rl.close(), 500);
        }
    });
}


//permet la lecture pas à pas
function afficher() {
    fs.readFile(myArgs[1], 'utf8', function(err, data) {
        if (err) {
            return console.log(err);
        }
        input = data.split(/\s/);
        while (i < 2)
        {
            isThisName();
        }
    });
}




function isThisName()
{

    var recup = input.shift();
    isThisBeginvCard(recup);
    if (recup == "")
    {
        i++;
    }
    else
    {
        console.log(recup);
        i = 0;
    }
}

//fonction qui permet de voir si c'est bien un nouveau contact
function isThisBeginvCard(maybe)
{
    if (maybe == "BEGIN:VCARD")
    {
        console.log("\n");
    }
}

//ajoute un contat au fichier vcard
function ajout(prenom, nom, tel, org, mail) {
    setTimeout(fs.appendFile('vcard.vcf', 'BEGIN:VCARD\r\n', function(err) {
        if (err)
            throw err;
    }), 1);
    setTimeout(fs.appendFile('vcard.vcf', 'VERSION:2.1\r\n', function(err) {
        if (err)
            throw err;
    }), 1);
    setTimeout(fs.appendFile('vcard.vcf', 'N:' + nom + ';' + prenom + ';;;\r\n', function(err) {
        if (err)
            throw err;
    }), 500);
    setTimeout(fs.appendFile('vcard.vcf', 'FN:' + prenom + " " + nom + '\r\n', function(err) {
        if (err)
            throw err;
    }), 2000);
    setTimeout(fs.appendFile('vcard.vcf', 'TEL;CELL:' + tel + '\r\n', function(err) {
        if (err)
            throw err;
    }), 3000);
    setTimeout(fs.appendFile('vcard.vcf', 'EMAIL;HOME:' + mail + '\r\n', function(err) {
        if (err)
            throw err;
    }), 4500);
    setTimeout(fs.appendFile('vcard.vcf', 'ORG:' + org + '\r\n', function(err) {
        if (err)
            throw err;
    }), 6000);
    setTimeout(fs.appendFile('vcard.vcf', 'END:VCARD\r\n', function(err) {
        if (err)
            throw err;
    }), 7000);
}

//
//
//dans le repertoire trunk:
//        svn commit -m "Ajout nouvelle version projet.js pour ma branche" --username pierre.davy92@gmail.com
//

//svn commit -m "ajout" --username pierre.davy92@gmail.com


// se positionner dans le bon repertoir
// node C:\Users\Pierre\Desktop\TravailUTT\GL02\pierre\clearwater\Spec1.js C:\Users\Pierre\Desktop\TravailUTT\GL02\pierre\clearwater\vcard.vcf


//
//BEGIN:VCARD
//VERSION:2.1
//N:Kennedy;John;;;
//FN:John Kennedy
//TEL;CELL:0698989898
//EMAIL;HOME:john.kennedy@gmail.com
//END:VCARD
//BEGIN:VCARD
//VERSION:2.1
//N:Mario;Bowser;;;
//FN:Bowser Mario
//TEL;CELL:0621222324
//EMAIL;PREF:Bowser.Mario@gmail.com
//END:VCARD
//BEGIN:VCARD
//VERSION:2.1
//N:Valjean;Jean;;
//FN:Jean Valjean
//TEL;CELL:0611111111
//EMAIL:jean.valjean@gmail.com
//END:VCARD
//BEGIN:VCARD
//VERSION:2.1
//N:Chirac;Jacques;;;
//FN:Jacques Chirac
//TEL;CELL:0679782188
//EMAIL:jacques.chirac@yahoo.fr
//END:VCARD

